#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDateTime>
#include <exception>
#include <QDebug>
#include <QMovie>
#include <QMessageBox>

// 排序算法参考地址：https://www.cnblogs.com/guoyaohua/p/8600214.html
// 算法参考地址：https://www.cs.usfca.edu/~galles/visualization/
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    _init();

    // 二分查找
    connect(ui->actionBinarySearch, &QAction::triggered,this,&MainWindow::onOpenBinarySearchDialog);

    // 冒泡排序
    connect(ui->actionBubbleSort, &QAction::triggered,this,&MainWindow::onOpenBubbleSortDialog);
}

MainWindow::~MainWindow()
{
    if(algorithmDialog != nullptr)
    {
        delete  algorithmDialog;
        algorithmDialog = nullptr;
    }
    delete ui;
}


void MainWindow::onAboutAuthor()
{
    QMessageBox msgBox;
    msgBox.setText(QStringLiteral("作者:千里小马哥\nQQ:291540704\n博客：https://blog.csdn.net/qianlixiaomage\n参考:\n(1)https://www.cnblogs.com/guoyaohua/p/8600214.html\n(2)https://www.cs.usfca.edu/~galles/visualization/"));
    msgBox.exec();
}

void MainWindow::_init()
{
    // MainWindow自身信号槽
    connect(ui->actionAboutAuthor, &QAction::triggered, this, &MainWindow::onAboutAuthor);

    ui->lineEdit->setPlaceholderText(QStringLiteral("当前选择算法"));
    ui->textEdit->setPlaceholderText(QStringLiteral("中间结果打印区"));

    // 状态栏时间
    currentTimeLabel = new QLabel(this);
    timer=new QTimer(this);
    timer->start(1000); // 每次发射timeout信号时间间隔为1秒

    connect(timer,&QTimer::timeout, this, &MainWindow::timeUpdate);

}

void MainWindow::onRecvOutput(QVector<QString> strVector)
{
    // 先关闭子窗口
    algorithmDialog->close();

    int _size =  strVector.size();
    for (int i=0; i<_size; i++) {
        QString str = strVector.at(i);
        ui->textEdit->append(str);
    }
}

void MainWindow::timeUpdate()
{
   QDateTime currentTime = QDateTime::currentDateTime();
   QString Timestr = currentTime.toString("yyyy年-MM月-dd日 hh:mm:ss "); //设置显示的格式
   currentTimeLabel->setText(QStringLiteral("  当前时间：") + Timestr);
   ui->statusbar->addWidget(currentTimeLabel);
}

void MainWindow::resetOutput()
{
    ui->lineEdit->clear();
    ui->textEdit->clear();
    if(movie != nullptr)
    {
        delete movie;
    }

    if(algorithmDialog != nullptr)
    {
        delete  algorithmDialog;
        algorithmDialog = nullptr;
    }

}

void MainWindow::_setNameAndMovie()
{
    ui->lineEdit->setText(algorithmDialog->algorithm);

    movie = new QMovie(algorithmDialog->algorithmMovie);
    ui->algorithmDesLabel->setMovie(movie);
    movie->setScaledSize(QSize(ui->algorithmDesLabel->width(), 250));
    movie->start();
}

void MainWindow::onOpenBinarySearchDialog()
{
    // 清空输出区
    this->resetOutput();
    // new出实际算法对话框类
    algorithmDialog = new BinarySearchDialog(QStringLiteral("二分查找算法"),QStringLiteral(":/movie/binarysearch.gif"), QSize(320, 240));
    // 绑定信号（执行算法信号->关闭子窗口）
    connect(algorithmDialog, &AlgorithmDialogBase::doWork, this, &MainWindow::onRecvOutput);

    // 初始化算法名参数
    this->_setNameAndMovie();

    algorithmDialog->show();
}

void MainWindow::onOpenBubbleSortDialog()
{
    this->resetOutput();
    // new出实际算法对话框类
    algorithmDialog = new BinarySearchDialog(QStringLiteral("冒牌排序算法"),QStringLiteral(":/movie/bubbleSort.gif"));
    // 绑定信号（执行算法信号->关闭子窗口）
    connect(algorithmDialog, &AlgorithmDialogBase::doWork, this, &MainWindow::onRecvOutput);

    // 初始化算法名参数
    this->_setNameAndMovie();

    algorithmDialog->show();
}
